package cn.lingyangwl.agile.food.core.service.impl;

import cn.lingyangwl.agile.common.security.utils.SecurityUtils;
import cn.lingyangwl.agile.food.core.assembly.TableAssembly;
import cn.lingyangwl.agile.food.core.manager.TableManager;
import cn.lingyangwl.agile.food.core.mapper.TableMapper;
import cn.lingyangwl.agile.food.model.entity.ShopTable;
import cn.lingyangwl.agile.food.model.entity.ShopTableCategory;
import cn.lingyangwl.agile.food.core.service.TableCategoryService;
import cn.lingyangwl.agile.food.core.service.TableService;
import cn.lingyangwl.agile.food.model.rqrs.table.TableListReq;
import cn.lingyangwl.agile.food.model.rqrs.table.TableResp;
import cn.lingyangwl.agile.food.model.rqrs.table.TableSaveReq;
import cn.lingyangwl.agile.food.model.rqrs.table.TableUpdateReq;
import cn.lingyangwl.framework.tool.core.exception.BizException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 店铺桌台
 *
 * @author shenguangyang
 */
@Slf4j
@Service
public class TableServiceImpl extends ServiceImpl<TableMapper, ShopTable>
    implements IService<ShopTable>, TableService {

    @Resource
    private TableManager tableManager;
    @Resource
    private TableAssembly tableAssembly;
    @Resource
    private TableCategoryService tableCategoryService;

    @Override
    public List<TableResp> listTable(TableListReq req) {
        LambdaQueryWrapper<ShopTable> lqw = ShopTable.lqw()
                .like(StringUtils.isNotEmpty(req.getCode()), ShopTable::getCode, req.getCode())
                .orderByDesc(ShopTable::getCreatedAt);
        List<ShopTable> list = this.list(lqw);
        List<Long> categoryIds = list.stream().map(ShopTable::getCategoryId).distinct().collect(Collectors.toList());
        categoryIds.add(-1L);

        Map<Long, String> categoryMap = tableCategoryService.listByIds(categoryIds).stream()
                .collect(Collectors.toMap(ShopTableCategory::getId, ShopTableCategory::getName));
        return list.stream()
    	        .map(tableAssembly::toTableResp).peek(e -> e.setCategoryName(categoryMap.get(e.getCategoryId())))
                .collect(Collectors.toList());
    }

    @Override
    public TableResp getTable(Long id) {
        ShopTable entity = this.getById(id);
    	return tableAssembly.toTableResp(entity);
    }

    public void validTable(Long id, String code) {
        if (StringUtils.isEmpty(code)) {
            return;
        }

        LambdaQueryWrapper<ShopTable> lqw = ShopTable.lqw().eq(ShopTable::getCode, code);
        // 处理保存或者更新
        lqw = Objects.isNull(id) ? lqw : lqw.ne(ShopTable::getId, id);
        if (this.count(lqw) > 0) {
            throw new BizException("桌码 {} 已存在", code);
        }
    }

    @Override
    public boolean saveTable(TableSaveReq req) {
        validTable(null, req.getCode());
        if (SecurityUtils.getLoginUser().getTenant().mainTenant()) {
            throw new BizException("不能在总店进行操作");
        }
        ShopTable entity = tableAssembly.toTable(req);
    	return this.save(entity);
    }

    @Override
    public boolean updateTable(TableUpdateReq req) {
        validTable(req.getId(), req.getCode());
        if (SecurityUtils.getLoginUser().getTenant().mainTenant()) {
            throw new BizException("不能在总店进行操作");
        }
        ShopTable entity = tableAssembly.toTable(req);
    	return this.updateById(entity);
    }

    @Override
    public boolean deleteTable(Long id) {
    	return this.removeById(id);
    }
    
    @Override
    public boolean batchDeleteTable(List<Long> ids) {
    	return this.removeByIds(ids);
    }
}
